const {
  $util,
} = getApp();
Component({
  externalClasses: ['cover-class'],
  properties: {
    isPrev: {
      type: Boolean,
      value: false,
    },
    imgList: Array, // 全路径
    img: Array, // 短路径
    maxCount: {
      type: Number,
      value: 9,
    },
  },
  data: {
    imgUrl: $util.getImgUrl(),
  },
  methods: {
    /**
     * 添加图片
     */
    _addImg() {
      const D = this.data;
      $util.upload({
        count: D.imgList.length ? D.maxCount - D.imgList.length : D.maxCount,
        success: (res) => {
          const imgList = D.imgList;
          const img = D.img;
          imgList.push(res.files[0].url);
          img.push(res.files[0].filename);
          this.setData({
            imgList,
            img,
          });
          this.triggerEvent('imgs', img, {});
          this.triggerEvent('lastimg', imgList, {});
        },
        progress: (p) => {
          console.log(p);
        },
      });
    },
    /**
     * 删除已选图片
     */
    _deleteImg(e) {
      const idx = e.currentTarget.dataset.idx;
      const D = this.data;
      const imgList = D.imgList;
      const img = D.img;
      imgList.splice(idx, 1);
      img.splice(idx, 1);

      this.setData({
        imgList,
        img,
      });
      this.triggerEvent('imgs', img, {});
      this.triggerEvent('lastimg', imgList, {});
    },
    _prevImg(e) {
      this.triggerEvent('prevImg', {}, {});
      const idx = e.currentTarget.dataset.idx;
      const D = this.data;
      const urls = D.imgList;
      wx.previewImage({
        urls,
        current: urls[idx],
      });
    },
  },
});
